package com.symaster.common.swing;

import javax.swing.*;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author yinmiao
 * @version 2022-12-31 11:04
 */
public abstract class BaseListener extends JComponent {

    private Set<Listener> listeners;

    public <T extends Listener> Set<T> getListener(Class<T> clazz) {
        if (listeners == null || listeners.size() == 0) {
            return null;
        }

        return listeners.stream().filter(clazz::isInstance).map(clazz::cast).collect(Collectors.toSet());
    }

    public void addListener(Listener listener) {
        if (listeners == null) {
            listeners = new HashSet<>();
        }
        listeners.add(listener);
    }

    public void removeListener(Listener listener) {
        if (listeners == null) {
            return;
        }
        listeners.remove(listener);
    }

    public interface Listener {

    }
}
